package com.a21200324.upalarm;

import android.app.Activity;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ViewFlipper;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

/**
 * Created by malmeida on 6/22/13.
 */
public class UpAlarm extends Activity implements LocationListener, View.OnTouchListener {

    private static final String TAG = "UpAlarm";

    private ViewFlipper vfWeather;
    private GestureDetector vfGesture;

    private LocationManager locationManager;
    private boolean gpsProviderEnable;
    private boolean networkProviderEnable;
    private String city;
    private String country;
    private String street;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_up_alarm);

        vfWeather = (ViewFlipper) findViewById(R.id.vf_weather);
        vfWeather.setOnTouchListener(this);
        vfGesture = new GestureDetector(this, new ViewFlipperGesture(this, vfWeather));

        locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);
        gpsProviderEnable = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        networkProviderEnable = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (gpsProviderEnable){
            //locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 1000, this);
        }else if (networkProviderEnable){
            //locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 1000, this);
        }
    }

    @Override
    public void onLocationChanged(Location location) {
        Log.d(TAG, "" + location.getLatitude());
        Log.d(TAG, "" + location.getLongitude());
        locationManager.removeUpdates(this);

        getAddress(location.getLatitude(), location.getLongitude());
        Log.d(TAG, "street: " + street);
        Log.d(TAG, "city: " + city);
        Log.d(TAG, "country: " + country);
    }

    @Override
    public void onStatusChanged(String s, int i, Bundle bundle) {

    }

    @Override
    public void onProviderEnabled(String s) {

    }

    @Override
    public void onProviderDisabled(String s) {

    }

    public void getAddress(double lat, double log){

        Geocoder geocoder = new Geocoder(this, Locale.getDefault());
        List<Address> addresses = null;

        try {

            addresses = geocoder.getFromLocation(lat, log, 1);

        } catch (IOException e1) {
            Log.e("LocationSampleActivity", "IO Exception in getFromLocation()");
            e1.printStackTrace();
        } catch (IllegalArgumentException e2) {
            e2.printStackTrace();
        }

        if (addresses != null && addresses.size() > 0) {
            Address address = addresses.get(0);

            street = address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : null;
            city = address.getLocality();
            country = address.getCountryName();

        }
    }

    @Override
    public boolean onTouch(View view, MotionEvent event) {

        if (view.getId() == R.id.vf_weather){
            return vfGesture.onTouchEvent(event);
        }

        return false;
    }
}